package cn.oa.service.Impl;


import cn.oa.dao.EmployeeDao;

import cn.oa.entity.Employee;
import cn.oa.entity.EmployeeVo;
import cn.oa.service.EmployeeService;
import cn.oa.util.MybatisUtil;
import cn.oa.util.Page;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    SqlSessionFactory sqlSessionFactory = MybatisUtil.sessionFactory;
    SqlSession sqlSession = sqlSessionFactory.openSession();
    EmployeeDao employeeDao=sqlSession.getMapper(EmployeeDao.class);
    @Override
    public Employee findEmployee(String employeeNo, String password) throws SQLException {
        Employee tmp= employeeDao.findEmployee(employeeNo,password);
        sqlSession.close();
        return tmp;
    }
    @Override
    public void getEmployeesByPage(String name, Page pageObj) throws SQLException {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        EmployeeDao employeeDao=sqlSession.getMapper(EmployeeDao.class);
        EmployeeVo vo=new EmployeeVo();
        vo.setName(name);
        vo.setBegIndeax(pageObj.getBginIndex());
        vo.setPageSize(pageObj.getPageSize());
        try {
            int totalCount = employeeDao.getEmployeesCount(vo);
            pageObj.setTotalCount(totalCount);
            if (totalCount > 0) {
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                }
                List<Employee> empList = employeeDao.getEmployeesByPage(vo);
                pageObj.setEmployeeList(empList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setEmployeeList(new ArrayList<Employee>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            sqlSession.close();
        }
    }
    @Override
    public int updateEmployee(Employee employee) throws SQLException {
        int ret=0;
        SqlSession sqlSession=sqlSessionFactory.openSession();
        EmployeeDao dao=sqlSession.getMapper(EmployeeDao.class);
        ret=dao.updateEmployee(employee);
        sqlSession.commit();
        sqlSession.close();
        sqlSession.rollback();
        return ret;
    }

    @Override
    public int addNewEmployee(Employee employee) throws SQLException {
        int ret=0;
        SqlSession sqlSession=sqlSessionFactory.openSession();
        EmployeeDao dao=sqlSession.getMapper(EmployeeDao.class);
        ret=dao.addNewEmployee(employee);
        sqlSession.commit();
        sqlSession.close();
        sqlSession.rollback();
        return ret;
    }

    @Override
    public Employee getEmployeeById(int id) throws SQLException {
        SqlSession sqlSession =sqlSessionFactory.openSession();
        EmployeeDao dao=sqlSession.getMapper(EmployeeDao.class);
        Employee tmp=dao.getEmployeeById(id);
        sqlSession.close();
        return tmp;
    }
    @Override
    public int deleteEmployeeById(int id) throws SQLException {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        EmployeeDao dao=sqlSession.getMapper(EmployeeDao.class);
        int tmp=dao.deleteEmployeeById(id);
        sqlSession.commit();
        sqlSession.rollback();
        sqlSession.close();
        return tmp;
    }

    @Override
    public int updatePassword(int id, String pwd) throws SQLException {
        Employee tmp = new Employee();
        tmp.setId(id);
        tmp.setPassword(pwd);
        return updateEmployee(tmp);
    }

}


